﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 花店管理系统.Tproduct
{
    public partial class Insert_product : Form
    {
        public Insert_product()
        {
            InitializeComponent();
        }
        public static string path_source="";
        SqlDataAdapter daProudct, dalog;
        DataSet ds = new DataSet();
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from Tgood";
            string sdr = "select * from log";
            daProudct = new SqlDataAdapter(str, DB.cn);
            dalog = new SqlDataAdapter(sdr, DB.cn);
            daProudct.Fill(ds, "product_info");
            dalog.Fill(ds, "log_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ValidateInput();
        }

        private void ValidateInput()
        {
            if (txtProductid.Text == "" || txtUnit.Text == "" || txtName.Text == "" || txtQty.Text == "")
            {
                MessageBox.Show("必填项不能为空");
            }
            else
            {
                DB.GetCn();
                string str = "select * from Tgood where G_id='" + txtProductid.Text + "'";
                DataTable dt = DB.GetDataSet(str);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("此商品已存在，请重新输入商品编号");
                }
                else
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\"
                        + cboCategory.Text + "\\";
                    fileFolder += filename;
                    AddProductToDataSet(filename, fileFolder);

                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["tname"] = Login.username;
                    drLog["L_type"] = "添加";
                    drLog["L_date"] = DateTime.Now;
                    drLog["L_table"] = "Tgood表";
                    ds.Tables["log_info"].Rows.Add(drLog);

                    try
                    {
                        SqlCommandBuilder dbProudct = new SqlCommandBuilder(daProudct);
                        daProudct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(dalog);
                        dalog.Update(ds, "log_info");
                        MessageBox.Show("添加成功");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);

                    }
                    DB.cn.Close();
                }
            }
        }

        private void AddProductToDataSet(string filename, string fileFolder)
        {
            DataRow drPro = ds.Tables["product_info"].NewRow();
            drPro["G_id"] = Convert.ToInt32(txtProductid.Text);
            drPro["L_id"] = cboCategory.SelectedValue;
            drPro["G_xiaoshoujia"] = decimal.Parse(txtPrice.Text);
            drPro["G_chenbenjia"] = decimal.Parse(txtUnit.Text);
            drPro["Y_id"] = cboSupp.SelectedValue;
            drPro["G_name"] = txtName.Text;
            drPro["G_miaosu"] = txtDescn.Text;
            drPro["G_kuchun"] = txtQty.Text;
            drPro["G_VIPjia"] = decimal.Parse(txtVIPPrice.Text);
            if (path_source != "")
            {
                File.Copy(path_source, fileFolder, true);
                drPro["Image"] = "\\Prod_Images\\" + cboCategory.SelectedText
                    + "\\" + filename;
            }
            else
            {
                drPro["Image"] = "暂无图片.png";
            }

            ds.Tables["product_info"].Rows.Add(drPro);
        }

        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog()==DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Insert_product_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“huadianDataSet.Tleibie”中。您可以根据需要移动或移除它
            this.tleibieTableAdapter.Fill(this.huadianDataSet.Tleibie);
            // TODO: 这行代码将数据加载到表“huadianDataSet2.Tgongyingshang”中。您可以根据需要移动或移除它。
            this.tgongyingshangTableAdapter.Fill(this.huadianDataSet2.Tgongyingshang);
            init();
            showAll();
        }
    }
}
